<?php

class NotebooksController extends Controller
{
        /**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}
        
        /**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
        public function accessRules()
	{
		return array(
			array('allow',  // allow authenticated user  to perform 'index','detail','editindex'
				'actions'=>array('index','detail','edit','editindex','addNotebook','delcate','delnote','updateDetail','createComment','movenote','updateCate'),
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
        
        /** 
         * list all notebook category with its notebooks inside
         */
	public function actionIndex()
	{
		$pid = Yii::app()->request->getParam('pid',1);
		if($pid==NULL)
		  throw new CHttpException(404, 'Param is not enough');
                // get all message category assocative with project
                $modelNoteBookCategories = NotebookCategory::model()->findAllByAttributes(array('notebook_category_project_id'=>$pid));
                $notebookCategories = array();
                foreach ($modelNoteBookCategories as $modelNoteBookCategory){
                    $listNotebooks = $modelNoteBookCategory->notebooks;
                    $notebookCategory['listNotebooks'] = $listNotebooks;
                    $notebookCategory['model']        = $modelNoteBookCategory;
                    $notebookCategories[] = $notebookCategory;
                }
                $this->render('index',array(
                    'notebookCategories'=>$notebookCategories,
                ));
	}
        
	public function actionAddNotebook() {
		// get param
                $pid = Yii::app()->request->getParam('pid');
                $notebookInputName = Yii::app()->request->getPost('notebookInputName');
                $selectCategory = Yii::app()->request->getPost('selectCategory');
                $content = Yii::app()->request->getPost('notebookContent');
                if ($pid==NULL)
		  throw new CHttpException(404, 'Param is not enough');
                
		// get model
                $notebookCategories = NotebookCategory::model()->findAllByAttributes(array("notebook_category_project_id"=>$pid));
                
                if (!isset($content)) {
                    $this->render('addNotebook', array('notebookCategories'=>$notebookCategories));
                    return;
                }
                
                if ($selectCategory == "-") {
                    $notebookCategory = new NotebookCategory();
                    $notebookCategory->notebook_category_name = Yii::app()->request->getPost('new-category-name');
                    $notebookCategory->notebook_category_project_id = $pid;
                    $notebookCategory->save();
                    $selectCategory = $notebookCategory->notebook_category_id;
                    Yii::log("fuck".$selectCategory);
                }
                
                //Save data
                $notebook = new Notebook();
                $notebook->notebook_name = $notebookInputName;
                $notebook->notebook_category_id = $selectCategory;
                $notebook->notebook_content = $content;
                $notebook->notebook_created_date = new CDbExpression('NOW()');
                $notebook->notebook_posted_by_user_id = Yii::app()->user->id;
                $notebook->save();
                
                Yii::log("1". $notebook->notebook_name."2".$notebook->notebook_category_id."3".$notebook->notebook_content."4".$notebook->notebook_updated_date);
                
                $nid = $notebook->notebook_id;
                
                $this->redirect(array('/notebooks/detail', 'pid'=>$pid,'nid'=>$nid));
	}
        
        public function actionDelcate() {
                $ctid = Yii::app()->request->getPost('ctid');
                
                $note = new Notebook();
                $note->deleteAllByAttributes(array('notebook_category_id'=>$ctid));
                $note->save();
                
                $noteCate = new NotebookCategory();
                $noteCate->deleteByPk($ctid);
                $noteCate->save();
        }
        
        public function actionDelNote() {
                $nid = Yii::app()->request->getPost('nid');
                
                $note = new Notebook();
                $note->deleteByPk($nid);
                $note->save();
        }
        
	public function actionDetail() {
                // get param
                $pid = Yii::app()->request->getParam('pid');
                $nid = Yii::app()->request->getParam('nid');
		if (($pid==NULL) or ($nid==NULL))
		  throw new CHttpException(404, 'Param is not enough');
                
		// request variable for view
                $notebook = Notebook::model()->findByPk($nid);
                $comments = Comment::model()->findAllByAttributes(array("comment_object_type"=>"NOTEBOOK","comment_object_id"=>$nid));
                $this->render('detail',array(
                    'notebook'=>$notebook,
                    'comments'=>$comments,
                ));
	}
	public function actionEdit() {
                // get param
                $nid = Yii::app()->request->getParam('nid');
                $pid = Yii::app()->request->getParam('pid');
                if (($pid==NULL) or ($nid==NULL))
		  throw new CHttpException(404, 'Param is not enough');
                $notebookInputName = Yii::app()->request->getPost('notebookInputName');
                $selectCategory = Yii::app()->request->getPost('selectCategory');
                $content = Yii::app()->request->getPost('notebookContent');
                
		// get model
                $notebook = Notebook::model()->findByPk($nid);
                $notebookCategories = NotebookCategory::model()->findAllByAttributes(array("notebook_category_project_id"=>$pid));
                
                if (!isset($content)) {
                    $this->render('edit', array('notebook'=>$notebook ,'notebookCategories'=>$notebookCategories));
                    return;
                }
                
                if ($selectCategory == "-") {
                    $notebookCategory = new NotebookCategory();
                    $notebookCategory->notebook_category_name = Yii::app()->request->getPost('new-category-name');
                    $notebookCategory->notebook_category_project_id = $pid;
                    $notebookCategory->save();
                    $selectCategory = $notebookCategory->notebook_category_id;
                    Yii::log("fuck".$selectCategory);
                }
                
                //Save data
                $notebook->notebook_name = $notebookInputName;
                $notebook->notebook_category_id = $selectCategory;
                $notebook->notebook_content = $content;
                $notebook->notebook_updated_date = new CDbExpression('NOW()');
                $notebook->save();
                
                $this->redirect(array('/notebooks/detail', 'pid'=>$pid,'nid'=>$nid));
	}
        
        /**
         * Update detail for a notebook
         * arg : $nid note id and note detail
         */
        public function actionUpdateDetail() {
                $nid = Yii::app()->request->getPost('nid');
                $detail = Yii::app()->request->getPost('detailName');
                
                if (($nid == null) or ($detail == null))
                    throw new CHttpException ('404', 'Param is not enough');
                
                $notebook = Notebook::model()->findByPk($nid);
                $notebook->notebook_name = $detail;
                $notebook->save();
        }
        
        /**
         * action for add new comment
         * return html code in view for new comment
         */
        public function actionCreateComment()
        {
                $this->layout = "/ajax/main";
                $uid = Yii::app()->user->id;
                $commentBody = Yii::app()->request->getPost("commentBody");
                $pid = Yii::app()->request->getPost("pid");
                $nid = Yii::app()->request->getPost("nid");
                
                $comment = new Comment();
                $comment->comment_content = $commentBody;
                $comment->comment_posted_date = new CDbExpression('NOW()');
                $comment->comment_posted_by_user_id = $uid;
                $comment->comment_project_id =$pid;
                $comment->comment_object_id = $nid;
                $comment->comment_object_type = "NOTEBOOK";
                $comment->save();
                
                $this->render('comment',array(
                    'comment'=>$comment,
                ));
        }
        
        /**
         * Move a notebook to another notebook category
         * 
         */
        
        public function actionMoveNote(){
                // get param
                $nid = Yii::app()->request->getPost("nid");
                $ctid = Yii::app()->request->getPost("ctid");
                
                // replace date
                $notebook = Notebook::model()->findByPk($nid);
                $notebook->notebook_category_id = $ctid;
                $notebook->save();
        }
        
        /**
         * Update category
         */
        public function actionUpdateCate() {
                // get param
                $ctid = Yii::app()->request->getPost("ctid");
                $ctname = Yii::app()->request->getPost("ctname");
                $ctdes = Yii::app()->request->getPost("ctdes");
                if (($ctid==NULL) or ($ctname==NULL) or ($ctdes==NULL))
		  throw new CHttpException(404, 'Param is not enough');
                
                $notebookCate = NotebookCategory::model()->findByPk($ctid);
                $notebookCate->notebook_category_name = $ctname;
                $notebookCate->notebook_category_description = $ctdes;
                $notebookCate->save();
        }
}